package com.zmn.oms.services.impl.quotation3;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.model.entity.quotation3.OrderQuotationProcess;
import com.zmn.oms.persistence.interfaces.quotation3.OrderQuotationProcessDao;
import com.zmn.oms.services.interfaces.quotation3.OrderQuotationProcessService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author sunlife
 * @date:2021/12/1 6:53 下午
 * description:
 */
@Service
public class OrderQuotationProcessServiceImpl implements OrderQuotationProcessService {
    @Resource
    OrderQuotationProcessDao orderQuotationProcessDao;

    @Override
    public int insert(OrderQuotationProcess orderQuotationProcess) {
        return orderQuotationProcessDao.insert(orderQuotationProcess);
    }

    @Override
    public OrderQuotationProcess findByOrderId(Long orderId) {
        return orderQuotationProcessDao.selectByOrderId(orderId);
    }

    @Override
    public OrderQuotationProcess findByOrderIdSrcMaster(Long orderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderQuotationProcessDao.selectByOrderId(orderId);
        }
    }

    @Override
    public int updateByPrimaryKeySelective(OrderQuotationProcess record) {
        return orderQuotationProcessDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByOrderIdSelective(OrderQuotationProcess record) {
        Integer count = orderQuotationProcessDao.countByOrderId(record.getOrderId());
        if (NumberUtil.isNullOrZero(count)) {
            return orderQuotationProcessDao.insertSelective(record);
        } else {
            return orderQuotationProcessDao.updateByOrderIdSelective(record);
        }
    }

    @Override
    public int updateUnSubmitStatusByOrderId(OrderQuotationProcess record) {
       return orderQuotationProcessDao.updateUnSubmitStatusByOrderId(record);
    }

    @Override
    public int updateFaultCodeByOrderId(OrderQuotationProcess record) {
        Objects.requireNonNull(record.getOrderId());
       return orderQuotationProcessDao.updateFaultCodeByOrderId(record);
    }

    @Override
    public int updateByOrderIdSelectiveSrcMaster(OrderQuotationProcess record) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            Integer count = orderQuotationProcessDao.countByOrderId(record.getOrderId());
            if (NumberUtil.isNullOrZero(count)) {
                return orderQuotationProcessDao.insertSelective(record);
            } else {
                return orderQuotationProcessDao.updateByOrderIdSelective(record);
            }
        }
    }

    @Override
    public int deleteByOrderId(Long orderId) {
        return orderQuotationProcessDao.deletebyOrderId(orderId);
    }
}
